package j180.javaoo.date;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class TestDate {
    public static void main(String[] args){
////        // 创建当前时间
////        Date d = new Date();
////        System.out.println(d);
////
////        String s = new SimpleDateFormat("YYYY-mm-dd hh:mm:ss").format(d);
////        System.out.println(s);
//
        // 创建当前日期
//        LocalDate ld = LocalDate.now();
//        System.out.println(ld.toString().substring(0, ld.toString().indexOf("-")));
//        System.out.println((int)(Math.random() * 1000));
//
//        LocalDate dl1 = LocalDate.of(2020, 11, 10);
//        System.out.println("指定日期："+ dl1);
//
//        // 根据字符串来创建时间
//        LocalDate ld2 = LocalDate.parse("2020-11-10");
//        System.out.println("指定日期：" + ld2);
//
//        // 根据自定义格式化创建日期
//        LocalDate localDate2 = LocalDate.parse("2020/11/10", DateTimeFormatter.ofPattern("yyyy/MM/dd"));
//  //      LocalDate localDate3 = LocalDate.parse("2021/03/15", DateTimeFormatter.ofPattern("yyyy/MM/dd"));
//        System.out.println("自定义格式指定时间：" + localDate2);
        f();

    }

    public static void f(){
        LocalDate l = LocalDate.now();
        LocalDate l2 = LocalDate.of(2019, 8, 9);

        Period p = Period.between(l,l2);
        String s = p.toString();
        System.out.println(Math.abs(Integer.parseInt(s.substring(s.indexOf('P') + 1, s.indexOf('Y')))));
        System.out.println(Math.abs(Integer.parseInt(s.substring(s.indexOf('Y') + 1, s.indexOf('M')))));
        System.out.println(Math.abs(Integer.parseInt(s.substring(s.indexOf('M') + 1, s.indexOf('D')))));
        System.out.println(p.getDays());
    }

    public static void g(){
        LocalDateTime ld1  = LocalDateTime.now();
        LocalDateTime ld2 = LocalDateTime.of(2018, 9, 20, 12, 13, 15);

        Duration d1 = Duration.between(ld1, ld2);
        String s = d1.toString();
        System.out.println(d1);
    }
}
